<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Insert title here</title>
        <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/css/style.css" />
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/jquery-easyui-1.4.3/themes/default/easyui.css" />
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/jquery-easyui-1.4.3/themes/icon.css" />
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/css/easyui.icon.css" />
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/jquery-easyui-1.4.3/jquery.min.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/jquery-easyui-1.4.3/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/jquery-easyui-1.4.3/locale/easyui-lang-zh_CN.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/jquery.js/jquery.json.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/jquery.js/jquery.fw.ext.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/js/jquery.form.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/easyui.js/easyui.ext.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/js/message.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/js/MD5.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/js/moment.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/js/zh-cn.js"></script>
    </head>
    <body>
        <div class="easyui-layout" data-options="fit:true">
            <div data-options="region:'north',split:true,border:false" style="height:50px;">
                <form id="searchForm">
                    <table>
                        <tr>
                        	<td>划分区域 <input class="easyui-combobox" style="width: 120px" id="modelName" name="modelName"></td>
                            <td><a id="btn_add" class="easyui-linkbutton" data-options="group:'g1',iconCls:'icon-add'">添加</a></td>
                            <td><a id="btn_dele" class="easyui-linkbutton" data-options="group:'g1',iconCls:'icon-remove'">删除</a></td>
                            <td><a onclick="grid.datagrid('reload');" href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-arrow_refresh'">刷新</a></td>
                        </tr>
                    </table>
                </form>
            </div>
            <div data-options="region:'center',border:false">
                <table id="grid">
                </table>
            </div>
            <div data-options="region:'east',split:true,border:false" style="width: 50%; padding: 5px;">
                <form id="form" name="form" method="post" class="form" enctype="multipart/form-data" action="${pageContext.request.contextPath}/addSysUpload.do">
                    <fieldset>
                    	<input id="funcId" name="funcId" style="display:none;" />
                        <legend> 基本信息--<span id="operateMode_title">添加模式</span> </legend>
                        <table class="table">
                            <tbody>
                                <tr>
                                    <th>ID</th>
                                    <td><input id="id" value="0" readonly="readonly" name="id" class="textbox"  style="height: 22px; width:100px; background:#eee;" /></td>
                                </tr>
                                <tr>
                                    <th>文件大小</th>
                                    <td><input id="fileSize" name="fileSize" class="textbox" readonly style="height: 22px; width:200px;background:#eee;"/></td>
                                </tr>
                                <tr>
                                    <th>文件</th>
                                    <td>
                                    	<input  id="fileFullName" name="fileFullName" class="textbox"  style="height:22px; width:300px;display:none;" />
                                    	<input id="file1" name="filePic1" class="textbox easyui-validatebox" required="true" style="height: 22px;margin:5px 0px; width:250px;" />                                    	
                                    	<input id="picUpLoad1" type="file" name="file1" style="display:none;" onchange="setPicName(1)" />
                                    	<a onclick="uploaderFun(1);"id="openFile" class="easyui-linkbutton">打开</a><br />
                                    </td>
                                </tr>
                                <tr>
                                	<th>所属产品</th>
                                	<td>
                                		主类：<input class="easyui-combobox" style="width: 120px" id="parentIdSch" name="parentIdSch" />
                                		副类：<input class="easyui-combobox" style="width: 120px" id="assistantType" name="assistantType" />
                                		产品：<input class="easyui-combobox" required="true" style="width: 200px" id="zipId" name="zipId" />
                                	</td>
                                </tr>
                                <tr>
                                	 <th>添加时间</th>
                                     <td>
                                        <input name="createdTime" class="easyui-datetimebox" style="width: 180px;" />
                                     </td>
                                </tr>
                                <tr>
                                    <th>序号</th>
                                    <td><input id="sortId" name="sortId"
                                            class="easyui-validatebox easyui-numberspinner"
                                            data-options="min:0,max:100,required:true" value="0"   style="width: 50px;" />
                                    </td>
                                </tr> 
                                <tr>    
                                    <th>操作</th>
                                	<td><a id="operateMode_submit" class="easyui-linkbutton" data-options="iconCls:'icon-save'">确定</a> <a id="operateMode_reset" class="easyui-linkbutton" data-options="iconCls:'icon-reload'">重置</a></td>
                                </tr>
                            </tbody>
                        </table>
                    </fieldset>
                </form>
            </div>
        </div>
        <script type="text/javascript">
        	var parentIdSch;
            $(function() {
                var gird;
                var modelName;
                var assistantType;
                var productItem;
                grid=$('#grid').datagrid({
                    fit:true,
                    border : false,
                    collapsible : false,
                    rownumbers : true,
                    pagination : true,
                    singleSelect : true,
                    rownumbers : true,
                    url:'',
                    columns:[[
            		                       
            			{ sortable:true,field:'id',title:'ID'}
            		                       
            			,{ sortable:true,field:'fileSize',title:'文件大小',formatter: function(value, row, index) {
                        	return parseInt(row.fileSize/1024)+"KB";
                        }}
            		                       
            			,{ sortable:true,field:'fileFullPath',title:'上传路径',width:180 }
            		                       
            			//,{ sortable:true,field:'imgUrl',title:'imgUrl'}
            		                       
            			//,{ sortable:true,field:'videoUrl',title:'videoUrl'}
            		                       
            			//,{ sortable:true,field:'linkUrl',title:'linkUrl'}
            			
            			,{ sortable:true,field:'sortId',title:'序号'}
            		                       
            			,{ sortable:true,field:'createdTime',title:'创建时间',
            				formatter: function(value, row, index){
    	            			return value.substring(0,16);
    	            		}	
            			}
            			
            		]],
                    onLoadSuccess : function(data) {
                        $('.iconImg').attr('src', fw.pixel_0);
                    },
                    onClickRow : function(index, row) {
                    	$("#form").form("reset");
                        $("#form").form("load", row);
                      	
                        $.ajax({
                        	url:'${pageContext.request.contextPath }/getMainAndAsistantTypeByProductId.do?id='+row.zipId,
                        	type:'post',
                        	success:function(data){
                        		if(data.success){
                        			$('#parentIdSch').combobox("select",data.mainTypeId);
                        			setTimeout(function(){
                        				$('#assistantType').combobox("select",data.assistantTypeId);
                        				setTimeout(function(){
                        					$('#zipId').combobox("select",row.zipId);
                            			},100);
                        			},100);
                        		}
                        	},
                        	dataType:'json'
                        })
                        //通过row.zipId获取
                        //格式化文件名
                        var fileFullPath = row.fileFullPath;
                        $("#file1").val(fileFullPath.substring(fileFullPath.lastIndexOf("/")+1));
                        //格式化文件大小
                        $("#fileSize").val(parseInt(row.fileSize/1024)+"KB");
                        $("#operateMode_title").html("修改模式");
                        //设置模块Id 
                        $("#funcId").val(row.funcId);
                    }
                });
                //初始化模块
                modelName = $('#modelName').combobox({
 					editable : false,
 					panelHeight : 'auto',
 					valueField : 'id',
 					textField : 'modelName',
 					data:[{id:'codeUpProduct',modelName:'二维码上方'},{id:'codeLeftProduct',modelName:'二维码左侧'}],
 					DropDownStyle: 'DropDownList',
 					onSelect : function(record) {
 						/*$("#parentId").val(record.id);
 						parentId = record.id;*/
 						$("#btn_add").click();
 						$("#funcId").val(record.id);
 						$('#grid').datagrid({
 		                     url: '${pageContext.request.contextPath }/listSysUpload.do?funcIdSch='+record.id
 		                });
 						$('#grid').datagrid('load',{});
 					},
 					onLoadSuccess : function(data) {
 						if (data.length > 0) {
 							//$('#modelName').combobox('select', data[0].id);
 						}
 					}
 				});
                
                //添加按钮事件
                $("#btn_add").click(function() {
                    $("#operateMode_title").html("添加模式");
                    $('#grid').datagrid("unselectAll");
                    $("#id").attr("value", 0);
                    var funcId = $("#funcId").val();
                    $("#form").form("reset");
                    $("#funcId").val(funcId);
                })
            
                //删除按钮事件
                $("#btn_dele").click(function() {
                    var rows = $('#grid').datagrid('getSelections');
                    if (rows <= 0) {
                        $.messager.show({
                            title : message.title.normal,
                            msg : message.grid_select,
                            timeout : message.timeout,
                            showType : message.showType
                        });
                    } else {
                        $.messager.confirm(message.title.askTitle, message.dele_comfirm, function(r) {
                            if (r) {
                                var idValue = $("#form input[name='id']").val();
                                $.post("${pageContext.request.contextPath }/delSysUpload.do", {
                                    id : idValue
                                }, function(result) {
                                    if (result.success) {
                                        $.messager.show({
                                            title : message.title.normal,
                                            msg : message.dele_success,
                                            timeout : message.timeout,
                                            showType : message.showType
                                        });
                                        grid.datagrid('reload');
                                    } else {
                                        $.messager.show({
                                            title : message.title.normal,
                                            msg : message.dele_fail,
                                            timeout : message.timeout,
                                            showType : message.showType
                                        });
                                    }
                                }, 'json');
                                $("#btn_add").click();
                            }
                        });
                    }
                })
            
                //确定按钮事件
                $("#operateMode_submit").click(function() {
                    var idValue = $("#form input[name='id']").val();
                    if ($('#form').form('validate')) {
                        if (idValue > 0) {
                        	//格式化文件大小数据
                        	$("#fileSize").val(0);
                            //修改
                            $.post("${pageContext.request.contextPath }/updateSysUpload.do", fw.serializeObject($('form')), function(result) {
                            	if (result.success) {
                                    $.messager.show({
                                        title : message.title.normal,
                                        msg : message.update_success,
                                        timeout : message.timeout,
                                        showType : message.showType
                                    });
            
                                    grid.datagrid("reload");
            
                                    //重新加载信息
                                    $("#form").form("load", "${pageContext.request.contextPath }/getSysUploadById.do?id=" + idValue);
                                    //格式化文件大小
                                    setTimeout(function(){
                                    	$("#fileSize").val(parseInt($("#fileSize").val()/1024)+"KB");
                                    },50);
                                }
                            }, 'json');
                        } else {
                            if(idValue.trim().length == 0){
                                $.messager.show({
                                    title : message.title.normal,
                                    msg : message.add_button_click,
                                    timeout : message.timeout,
                                    showType : message.showType
                                });
                            }else{
                            	//新增
                            	if($("#funcId").val()!= null && $("#funcId").val() != ""){
                            		$("#form").ajaxSubmit(function(result) {
                                		result = JSON.parse(result);
                                		if(result.success){
                                			 $.messager.show({
                                                 title : message.title.normal,
                                                 msg : message.add_success,
                                                 timeout : message.timeout,
                                                 showType : message.showType
                                             });
                                             grid.datagrid("reload");
                                             $("#form").form("reset");
                                		}else{
                                        	$.messager.show({
                                                title : message.title.normal,
                                                msg : message.add_fail,
                                                timeout : message.timeout,
                                                showType : message.showType
                                            });
                                        }
                                	});
                            	}else{
                            		$.messager.show({
                                        title : message.title.normal,
                                        msg : '请先选择所属区域！！！',
                                        timeout : message.timeout,
                                        showType : message.showType
                                    });
                            	}
                                //新增
                                /**$.post("${pageContext.request.contextPath }/addSysUpload.do", fw.serializeObject($('form')), function(result) {
                                	if (result.success) {
                                        $.messager.show({
                                            title : message.title.normal,
                                            msg : message.add_success,
                                            timeout : message.timeout,
                                            showType : message.showType
                                        });
                                        grid.datagrid("reload");
                                        $("#form").form("reset");
                                    }
                                }, 'json');**/
                            }
                        }
                    }
                })
                //重置按钮操作
                $("#operateMode_reset").click(function() {
                    var idValue = $("#form input[name='id']").val();
                    if (idValue > 0) {
                        $("#form").form("load","${pageContext.request.contextPath }/getSysUploadById.do?id=" + idValue);
                    } else {
                        $("#form").form("reset");
                    }
                })
                
              //初始化产品主类
                $.getJSON('${pageContext.request.contextPath }/listAllProducttype.do?parentIdSch=0',function(productJson) {
   	            	if(productJson.rows.length==0){
   	            		parentIdSch = $('#parentIdSch').combobox();
   	            		parentIdSch.combobox("clear");
   	            	}else{
   	            		parentIdSch = $('#parentIdSch').combobox({
	   	 					editable : false,
	   	 					panelHeight : 'auto',
	   	 					valueField : 'id',
	   	 					textField : 'productTypeName',
	   	 					data:productJson.rows,
	   	 					DropDownStyle:'DropDownList',
	   	 					onSelect : function(record) {
	   	 						/*$("#parentId").val(record.id);
	   	 						parentId = record.id;
	   	 						$("#btn_add").click();
	   	 						$('#grid').datagrid({
	   	 		                     url: '${pageContext.request.contextPath }/listProducttype.do?parentIdSch='+parentId
	   	 		                });
	   	 						$('#grid').datagrid('load',{});*/
	   	 						//初始化产品副类
	   	 						$.getJSON('${pageContext.request.contextPath }/listProducttype.do?parentIdSch='+record.id,function(assistantJson) {
		   	 						if(assistantJson.rows.length==0){
		   	 							assistantType = $('#assistantType').combobox();
		   	 							assistantType.combobox("clear");
		   	    	            	}else{
		   	    	            		assistantType = $('#assistantType').combobox({
			   	 	   	 					editable : false,
			   	 	   	 					panelHeight : 'auto',
			   	 	   	 					valueField : 'id',
			   	 	   	 					textField : 'productTypeName',
			   	 	   	 					data:assistantJson.rows,
			   	 	   	 					DropDownStyle:'DropDownList',
			   	 	   	 					onSelect : function(record) {
			   	 	   	 				
					   	 	   	 				//初始化产品副类
					   	 						$.getJSON('${pageContext.request.contextPath }/listAllProduct.do?productTypeIdSch='+record.id,function(productItemJson) {
						   	 						if(productItemJson.rows.length==0){
						   	 							productItem = $('#zipId').combobox();
						   	 							productItem.combobox("clear");
						   	    	            	}else{
						   	    	            		productItem = $('#zipId').combobox({
							   	 	   	 					editable : false,
							   	 	   	 					panelHeight : 'auto',
							   	 	   	 					valueField : 'id',
							   	 	   	 					textField : 'productName',
							   	 	   	 					data:productItemJson.rows,
							   	 	   	 					DropDownStyle:'DropDownList',
							   	 	   	 					onSelect : function(record) {
							   	 	   	 						
							   	 	   	 					}
							   	 	   	 				});
						   	 						}
					   	 						});
			   	 	   	 					}
		   	    	            		});
		   	    	            	}
	   	 						});
	   	 					},
	   	 					onLoadSuccess : function(data) {
	   	 						if (data.length > 0) {
	   	 							$('#parentIdSch').combobox('select', data[0].id);
	   	 						}
	   	 					}
	   	 				});
   	            	}
   	             }); 
            });
            
            function uploaderFun(index) {
        	    $("#picUpLoad"+index).trigger("click");
        	}
            
            function setPicName(index){
            	var picName = $("#picUpLoad"+index).val();
            	picName = picName.substring(picName.lastIndexOf("\\")+1);
            	$("#file"+index).val(picName);
            	$("#file"+index).trigger("click");
            }
        </script>
    </body>
</html>